﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.LotterySrv;
using SLLottery.Common;

namespace SLLottery
{
    public partial class LowerWindow : ChildWindow
    {
        private bool isAdd = false;
        LowerInfo lowerInfo;
        private double lowerBackPct = 0.0;
        private const double MaxGiveBackOfNewAccount = 0.035;
        private const double MinGiveBackSpan = 0.005;
        private string lowerRealName = "";
        User user = (User)App.Session[Constant.USERINFO_SESSION];
        LotterySrv.LotteryServiceClient srv;
        double _agentBackPct;
        double _parentBonus;
        public LowerWindow()
        {
            InitializeComponent();
            lowerInfo = new LowerInfo();
            srv = WcfProxy.GetProxy;
            srv.GetOverAllCfgCompleted += new EventHandler<GetOverAllCfgCompletedEventArgs>(srv_GetOverAllCfgCompleted);
            srv.AddLowerInfoCompleted += new EventHandler<AddLowerInfoCompletedEventArgs>(srv_AddLowerInfoCompleted);
            srv.EditLowerInfoCompleted += new EventHandler<EditLowerInfoCompletedEventArgs>(srv_EditLowerInfoCompleted);
        }

        void srv_GetOverAllCfgCompleted(object sender, GetOverAllCfgCompletedEventArgs e)
        {
            this.gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result.Keys.Contains("MaxBonusPct"))
                Constant.MaxBonusPct = double.Parse(e.Result["MaxBonusPct"]);
            if(isAdd)
                this.tbBackPct.Maximum = _agentBackPct - Constant.MemBonusIncrement > Constant.MaxBonusPct ?
                Constant.MaxBonusPct : _agentBackPct - Constant.MemBonusIncrement;
            else
                this.tbBackPct.Maximum = _parentBonus - Constant.MemBonusIncrement > Constant.MaxBonusPct ? 
                    Constant.MaxBonusPct : _parentBonus - Constant.MemBonusIncrement;
            
        }

        void srv_EditLowerInfoCompleted(object sender, EditLowerInfoCompletedEventArgs e)
        {
            this.gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result)
            {
                this.DialogResult = true;
            }
            else
            {
                lbErrorMsg.Text = "网络故障或者服务器繁忙!";
            }
        }

        void srv_AddLowerInfoCompleted(object sender, AddLowerInfoCompletedEventArgs e)
        {
            this.gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result)
            {
                this.DialogResult = true;
            }
            else
            {
                lbErrorMsg.Text = "已经存在的会员ID!";
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.lbErrorMsg.Text = "";
            if (this.isAdd)
            {
                lowerInfo.UserID = tbUserName.Text.Trim();
                if (string.IsNullOrWhiteSpace(lowerInfo.UserID))
                {
                    this.lbErrorMsg.Text = "用户名不能为空";
                    return;
                }
                if (lowerInfo.UserID.Length > 20)
                {
                    this.lbErrorMsg.Text = "用户名过长(10字符以内)";
                    return;
                }
                if(!Common.Common.ValidateUserID(lowerInfo.UserID))
                {
                    this.lbErrorMsg.Text = "用户名只允许输入数字、汉字、英文、下划线";
                    return;
                }
                lowerInfo.UserPwd = this.tbPassword.Password;
                if (string.IsNullOrWhiteSpace(lowerInfo.UserPwd))
                {
                    lbErrorMsg.Text = "密码不能为空";
                    return;
                }
                if (lowerInfo.UserPwd.Length < 6)
                {
                    lbErrorMsg.Text = "密码太短(6位以上)";
                    return;
                }
                if (lowerInfo.UserPwd != this.tbPassword1.Password)
                {
                    lbErrorMsg.Text = "两次输入的密码不一致";
                    return;
                }
                if ((!Common.Common.ValidateInput(lowerInfo.UserPwd)) || (!Common.Common.ValidateInput(this.tbPassword1.Password)))
                {
                    this.lbErrorMsg.Text = "密码只允许输入数字、英文、下划线";
                    return;
                }
                lowerInfo.Bonus = this.tbBackPct.Value;
                lowerInfo.RoleID = this.rbtAgent.IsChecked.Value ? 2 : 3;
                lowerInfo.FullPath = user.FullPath + user.ID + "/";
                lowerInfo.ParentUID = user.ID;
                this.gridLoading.Visibility = Visibility.Visible;
                srv.AddLowerInfoAsync(lowerInfo);
            }
            else
            {
                if (this.tbBackPct.Value == lowerInfo.Bonus)
                {
                    this.lbErrorMsg.Text = "未修改任何内容!";
                    return;
                }
                lowerInfo.Bonus = this.tbBackPct.Value;
                this.gridLoading.Visibility = Visibility.Visible;
                srv.EditLowerInfoAsync(lowerInfo);
            }
        }
        public void ShowAddWindow(double agentBackPct)
        {
            this.gridLoading.Visibility = Visibility.Visible;
            //this.tbBackPct.Increment = Constant.MemBonusIncrement;
            _agentBackPct = agentBackPct;
            this.tbBackPct.Increment = 0.5;
            this.lbErrorMsg.Text = "";
            this.isAdd = true;
            this.gridLoading.Visibility = Visibility.Collapsed;
            this.gridMain.Visibility=Visibility.Visible;
            base.Title = "添加下级";
            this.rbtAgent.IsEnabled=true;
            this.rbtAgent.IsChecked=true;
            this.rbtMember.IsEnabled=true;
            this.tbUserName.IsReadOnly = false; ;
            this.tbPassword1.Password="";
            this.tbPassword.Password = "";
            this.tbUserName.Text="";
            this.spPassword1.Visibility=Visibility.Visible;
            this.lbPassword1.Visibility = Visibility.Visible;
            this.spPassword.Visibility = Visibility.Visible;
            this.lbPassword.Visibility = Visibility.Visible;
            this.tbBackPct.Minimum = 0.0;
            this.tbBackPct.Maximum = agentBackPct - Constant.MemBonusIncrement > Constant.MaxBonusPct ?
                Constant.MaxBonusPct : agentBackPct - Constant.MemBonusIncrement;
            //this.tbBackPct.Maximum = Math.Min(0.035, Math.Max((double)0.0, (double)(agentBackPct - 0.005))) * 100.0;
            this.tbBackPct.Value = this.tbBackPct.Minimum;
            this.lbGiveBackDesc.Text=string.Format("{0:F2}% - {1:F2}%", this.tbBackPct.Minimum, this.tbBackPct.Maximum);
            base.Show();
            srv.GetOverAllCfgAsync();
        }

        public void ShowEditWindow(long uid,double parentBonus)
        {
            this.gridLoading.Visibility = Visibility.Visible;
            _parentBonus = parentBonus;
            srv.GetLowerListDataAsync(uid);
            srv.GetLowerListDataCompleted += (s, e) =>
                {
                    grid.RowDefinitions.Clear();
                    grid.RowDefinitions.Insert(0, new RowDefinition());
                    grid.RowDefinitions.Insert(1, new RowDefinition());
                    grid.RowDefinitions.Insert(2, new RowDefinition());
                    tb_Bonus.SetValue(Grid.RowProperty, 2);
                    sp_Bonus.SetValue(Grid.RowProperty, 2);
                    tbUserName.IsReadOnly = true;
                    LowerListData ld=e.Result.FirstOrDefault();
                    this.Title = "修改下级";
                    this.Width = 284;
                    this.Height = 160;
                    if (ld.RoleName == "会员")
                        rbtMember.IsChecked = true;
                    else
                        rbtAgent.IsChecked = true;
                    rbtAgent.IsEnabled = false;
                    rbtMember.IsEnabled = false;

                    tbUserName.Text = ld.UserID;

                    lbPassword.Visibility = Visibility.Collapsed;
                    spPassword.Visibility = Visibility.Collapsed;
                    this.spPassword1.Visibility = Visibility.Collapsed;
                    this.lbPassword1.Visibility = Visibility.Collapsed;

                    grid.Children.Remove(lbPassword);
                    grid.Children.Remove(spPassword);
                    grid.Children.Remove(spPassword1);
                    grid.Children.Remove(lbPassword1);

                    //this.tbBackPct.Increment = Constant.MemBonusIncrement;
                    this.tbBackPct.Increment = 0.5;
                    this.tbBackPct.Minimum = ld.Bonus;
                    this.tbBackPct.Maximum = parentBonus - Constant.MemBonusIncrement > Constant.MaxBonusPct ? Constant.MaxBonusPct : parentBonus - Constant.MemBonusIncrement;
                    this.tbBackPct.Value = ld.Bonus;
                    lowerInfo.Bonus = ld.Bonus;
                    lowerInfo.UserID = ld.UserID;
                    this.lbGiveBackDesc.Text = string.Format("{0:F2}% - {1:F2}%", this.tbBackPct.Minimum, this.tbBackPct.Maximum);
                    base.Show();
                    srv.GetOverAllCfgAsync();
                };
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

